from ultralytics.data.augment import LetterBox
from ultralytics.data import YOLODataset
from ultralytics.cfg import cfg2dict
import cv2

# 实验数据格式转化
config = cfg2dict('./coco8/coco8.yaml')
dataset = YOLODataset(data=config, img_path='./coco8/images')
idx = 1
img = dataset.get_image_and_label(idx)['img']

# LetterBox类初始化
letterbox = LetterBox(new_shape=(1280,1280), auto=False, scaleFill=True, scaleup=False,center=False, stride=32)

result_img = letterbox(image=img)
# 可视化保存
savedir = './letterbox_test.jpg'
cv2.imwrite(savedir, result_img)
